package com.yc.java_basic3;

import java.util.Random;
import java.util.Scanner;

public class Link {
	//作用域     在方法里面定义的变量,称之为:局部变量, 局部变量,只能在当前方法里面访问
	//在类里面定义的变量,称之为:全局变量. 只要在这个类里面,任何地方都可以访问
		//static 先不管
	public static Scanner sc = new Scanner(System.in);
	public static Random r = new Random();
	public static int[][] map;			//游戏地图
	public static int row;		//地图的大小
	public static int col;
	public static boolean isGameOver = false;
	
	public static void main(String[] args) {
		System.out.println("欢迎来到简易版连连看");
		System.out.println("请选择您的难度: 1.简单    2.普通    3.困难");
		int choice = sc.nextInt();		//难度控制数组的大小
		if( choice == 1 ) {
			row = 4;
			col = 4;
		}else if( choice == 2 ) {
			row = 6;
			col = 6;
		}else {
			row = 10;
			col = 10;
		}
		//行和列确定好了,那么,地图大小也就确定好了
		map = new int[row][col];
		
		//随机值
		for(int i = 0; i < map.length; i++) {
			for(int j = 0; j < map[i].length; j+=2) {	//一次过两个
				//我们要保证, 随机出来的数字, 是偶数个的
				int num = r.nextInt(9) + 1;
				map[i][j] = num;
				map[i][j + 1] = num;
			}
		}
		
		//打乱顺序
		for(int i = 0; i < row * col; i++) {
			int x1 = r.nextInt(row);
			int y1 = r.nextInt(col);
			int x2 = r.nextInt(row);
			int y2 = r.nextInt(col);
			
			//经典的两个数交换位置
			int temp = map[x1][y1];
			map[x1][y1] = map[x2][y2];
			map[x2][y2] = temp;
		}
		
		//提示用户输入要消除的两个点
		int count = 0;
		while( !isGameOver ) {
			//开始显示
			show();
			
			System.out.println("请输入您要消除的两个点的坐标: ");
			int x1 = sc.nextInt() - 1;
			int y1 = sc.nextInt() - 1;
			int x2 = sc.nextInt() - 1;
			int y2 = sc.nextInt() - 1;
			//判断能不能消除
			if( x1 == x2 && y1 == y2 ) {
				System.out.println("同一个点,不能消除");
			}else if( map[x1][y1] != map[x2][y2] ) {
				System.out.println("值不相同,不能消除");
			}else {
				//消除情况
					// 单线消除  传参数           双线消除                   三线消除
				if( oneLine(x1,y1,x2,y2) || twoLine(x1,y1,x2,y2) || threeLine(x1,y1,x2,y2) ) {
					map[x1][y1] = 0;
					map[x2][y2] = 0;
					
					count++;
					if( count >= row * col / 2 ) {
						System.out.println("恭喜通关");
						isGameOver = true;
					}
				}else {
					System.out.println("规则不符,不能消除");
				}
			}
		}
		
	}

	//三线消除
	private static boolean threeLine(int x1, int y1, int x2, int y2) {
		int zx1 = x1;
		int zy1;
		for(int i = 0; i < col; i++) {
			zy1 = i;
			//分解     A-B的三线,分解为: A-Z的单线 Z-B的双线
			if( map[zx1][zy1] == 0 && oneLine(x1,y1,zx1,zy1) && twoLine(zx1,zy1,x2,y2) ) {
				return true;
			}
		}
		
		int zx2;
		int zy2 = y1;
		for(int i = 0; i < row; i++) {
			zx2 = i;
			if( map[zx2][zy2] == 0 && oneLine(x1,y1,zx2,zy2) && twoLine(zx2,zy2,x2,y2) ) {
				return true;
			}
		}
		return false;
	}

	//双线消除
	private static boolean twoLine(int x1, int y1, int x2, int y2) {
		//基于单线消除
		//先定义转折点
		int zx1 = x1;
		int zy1 = y2;
		
		//A-B的双线消除,就转换为: A-Z的单线, Z-B的单线
		if( map[zx1][zy1] == 0 && oneLine(x1,y1,zx1,zy1) && oneLine(zx1,zy1,x2,y2) ) {
			return true;
		}
		
		int zx2 = x2;
		int zy2 = y1;
		if( map[zx2][zy2] == 0 && oneLine(x1,y1,zx2,zy2) && oneLine(zx2,zy2,x2,y2) ) {
			return true;
		}
		
		return false;
	}

	//单线消除
	private static boolean oneLine(int x1, int y1, int x2, int y2) {
		//只有两种情况: 同行 或者 同列
		if( x1 == x2 ) {
			//应该从小的,循环到大的才行
			int min = y1 < y2 ? y1 : y2;
			int max = y1 > y2 ? y1 : y2;
			for(int i = min + 1; i < max; i++) {
				if( map[x1][i] != 0 ) {
					return false;		//中间有其他的值,不能消除
				}
			}
			return true;
		}else if( y1 == y2 ){
			int min = Math.min(x1, x2);
			int max = Math.max(x1, x2);
			for(int i = min + 1; i < max; i++) {
				if( map[i][y1] != 0 ) {
					return false;
				}
			}
			return true;
		}
		return false;
	}

	//定义方法
	public static void show() {
		System.out.println();
		for(int i = 0; i < map.length; i++) {
			for(int j = 0; j < map[i].length; j++) {
				//0不显示
				if( map[i][j] == 0 ) {
					System.out.print( " " + " " );
				}else {
					System.out.print( map[i][j] + " " );
				}
			}
			System.out.println();
		}
	}
}
